package com.atguigu.atcrowdfunding.manager.controller;

import com.atguigu.atcrowdfunding.bean.Member;
import com.atguigu.atcrowdfunding.potal.service.MemberService;
import com.atguigu.atcrowdfunding.potal.service.TicketService;
import com.atguigu.atcrowdfunding.util.AjaxResult;
import com.atguigu.atcrowdfunding.util.Page;
import javafx.beans.binding.ObjectExpression;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xzt
 * @create 2021-03-16 20:04
 */
@Controller
@RequestMapping("/authcert")
public class AuthCertController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TicketService ticketService;

    @Autowired
    private MemberService memberService;

    @RequestMapping("/index")
    public String index() {
        return "authcert/index";
    }

    @ResponseBody
    @RequestMapping("/pageQuery")
    public Object pageQuery(@RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        AjaxResult ajaxResult = new AjaxResult();
        try {

            Page page = new Page(pageNo, pageSize);

            //1、查询后台backuser委托组的任务
            TaskQuery taskQuery = taskService.createTaskQuery().processDefinitionKey("auth")
                    .taskCandidateGroup("backuser");

            List<Task> tasks = taskQuery.listPage(page.startIndex(), page.getPageSize());


            List<Map<String, Object>> data = new ArrayList<>();

            for (Task task : tasks) {
                Map<String, Object> map = new HashMap<>();

                map.put("taskid", task.getId());
                map.put("taskName", task.getName());

                //2、根据任务查询流程定义(获取流程定义名称和版本)
                ProcessDefinition processDefinition = repositoryService
                        .createProcessDefinitionQuery()
                        .processDefinitionId(task.getProcessDefinitionId())
                        .singleResult();

                map.put("procDefName", processDefinition.getName());
                map.put("procDefVersion", processDefinition.getVersion());

                //3、根据任务来查询流程实例(根据流程实例的id查询流程单，查询用户的信息)
                Member member = ticketService.getMemberByPiid(task.getProcessInstanceId());

                map.put("member", member);
                data.add(map);
            }

            page.setDatas(data);
            Long count = taskQuery.count();
            page.setTotalSize(count.intValue());
            ajaxResult.setPage(page);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("审核任务分页查询失败!");
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/show")
    public String show(Integer memberid, Model model) {

        Member member = memberService.getMemberById(memberid);

        List<Map<String, Object>> list = memberService.queryCertByMemberid(memberid);

        model.addAttribute("member", member);
        model.addAttribute("certimgs", list);
        System.out.println(list);
        return "authcert/show";
    }

    @ResponseBody
    @RequestMapping("/pass")
    public Object pass(String taskId, Integer memberId) {
        AjaxResult result = new AjaxResult();

        try {
            taskService.setVariable(taskId, "flag", true);
            taskService.setVariable(taskId, "memberid", memberId);
            // 传递参数，让流程继续执行
            taskService.complete(taskId);
            result.setSuccess(true);
        } catch ( Exception e ) {
            e.printStackTrace();
            result.setSuccess(false);
        }

        return result;
    }

    @ResponseBody
    @RequestMapping("/refuse")
    public Object refuse(String taskId, Integer memberId) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            //传递参数，让流程继续执行
            taskService.setVariable(taskId, "flag", false);
            taskService.setVariable(taskId, "memberid", memberId);

            ajaxResult.setSuccess(true);
            taskService.complete(taskId);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("审批操作失败");
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
